package com.ruoyi.repair.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.repair.domain.CheMaintain;
import com.ruoyi.repair.service.ICheMaintainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 维修管理（其实用不到）Controller
 * 
 * @author 侯稼骏
 * @date 2024-12-31
 */
@RestController
@RequestMapping("/maintain")
public class CheMaintainController extends BaseController
{
    @Autowired
    private ICheMaintainService cheMaintainService;

    /**
     * 查询维修管理（其实用不到）列表
     */
    @RequiresPermissions("repair:maintain:list")
    @GetMapping("/list")
    public TableDataInfo list(CheMaintain cheMaintain)
    {
        startPage();
        List<CheMaintain> list = cheMaintainService.selectCheMaintainList(cheMaintain);
        return getDataTable(list);
    }

    /**
     * 导出维修管理（其实用不到）列表
     */
    @RequiresPermissions("repair:maintain:export")
    @Log(title = "维修管理（其实用不到）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheMaintain cheMaintain)
    {
        List<CheMaintain> list = cheMaintainService.selectCheMaintainList(cheMaintain);
        ExcelUtil<CheMaintain> util = new ExcelUtil<CheMaintain>(CheMaintain.class);
        util.exportExcel(response, list, "维修管理（其实用不到）数据");
    }

    /**
     * 获取维修管理（其实用不到）详细信息
     */
    @RequiresPermissions("repair:maintain:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheMaintainService.selectCheMaintainById(id));
    }

    /**
     * 新增维修管理（其实用不到）
     */
    @RequiresPermissions("repair:maintain:add")
    @Log(title = "维修管理（其实用不到）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheMaintain cheMaintain)
    {
//        添加维修单号
        if(cheMaintain.getNumber() == null || cheMaintain.getNumber().isEmpty()){
            cheMaintain.setNumber(UUID.randomUUID().toString().replace("-", "").substring(0,7));
        }
//        添加时间
        if(cheMaintain.getSubmitTime() == null || cheMaintain.getSubscribeTime() == null){
            cheMaintain.setSubmitTime(DateUtils.getNowDate());
        }
        Long i = cheMaintainService.insertCheMaintain(cheMaintain);
        if(i == 0){
            return error();
        }else {
            return success(i);
        }
    }

    /**
     * 修改维修管理（其实用不到）
     */
    @RequiresPermissions("repair:maintain:edit")
    @Log(title = "维修管理（其实用不到）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheMaintain cheMaintain)
    {
        return toAjax(cheMaintainService.updateCheMaintain(cheMaintain));
    }

    /*
     *
     * @author ls
     * @date 2025/1/13 6:21
     * 修改状态
     */
    @PostMapping("/updateState")
    public AjaxResult updateState(@RequestBody CheMaintain cheMaintain)
    {
        return toAjax(cheMaintainService.updateCheMaintainState(cheMaintain));
    }

    /**
     * 删除维修管理（其实用不到）
     */
    @RequiresPermissions("repair:maintain:remove")
    @Log(title = "维修管理（其实用不到）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(cheMaintainService.deleteCheMaintainById(id));
    }
}
